package com.atguigu.gmall.seckill.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.seckill.service.biz.SeckillBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//api/activity/seckill/auth/getSeckillSkuIdStr/56
@RequestMapping("/api/activity/seckill/auth")
@RestController
public class SeckillRestController {


    @Autowired
    SeckillBizService seckillBizService;


    /**
     * 获取一个商品的秒杀码
     * @param skuId
     * @return
     */
    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable("skuId") Long skuId){

        //生成秒杀码。
        String code = seckillBizService.generateSeckillCode(skuId);

        return Result.ok(code);
    }


    /**
     * 秒杀下单
     * @param skuId
     * @param code
     * @return
     */
    @PostMapping("/seckillOrder/{skuId}")
    public Result createSeckillOrder(@PathVariable("skuId") Long skuId,
                                     @RequestParam("skuIdStr") String code){


        //创建秒杀单
        seckillBizService.createSeckillOrder(skuId,code);

        //每3s查询一次秒杀的订单状态
        return Result.ok();
    }

    /**
     * api/activity/seckill/auth/checkOrder/56
     * 检查秒杀单状态
     */
    @GetMapping("/checkOrder/{skuId}")
    public Result checkSeckillOrderStatus(@PathVariable("skuId") Long skuId){

        ResultCodeEnum resultCodeEnum =  seckillBizService.checkSeckillOrder(skuId);

        return Result.build(null,resultCodeEnum);
    }

    //api/activity/seckill/auth/submitOrder
    @PostMapping("/submitOrder")
    public Result submitSeckillOrder(@RequestBody OrderInfo orderInfo){

        //订单服务创建出订单，并返回订单id

        //以前的创建订单流程都一样。  这次订单创建没有token
        //TODO 远程生成订单保存到数据库、得到订单id即可
//        long orderId = new Random().nextLong();


        return Result.ok("714064473765183488");
    }
}
